package com.soar.tbox.util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.soar.tbox.Constants;

import android.os.Environment;

/**
 * 配置文件辅助类
 * @author songjunjie 
 *
 */
public class SettingHelper {
	private static Map<String,String> setting = new HashMap<String,String>();
	private static void init() throws Exception{
		String sdPath = Environment.getExternalStorageDirectory().getPath();
		String commandPath = sdPath + File.separator + Constants.COMMAND_DIR_NAME;
		File settingFile = new File(commandPath+File.separator+Constants.SETTING_FILENAME); 
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document = reader.read(settingFile);
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new Exception("读取文件" + settingFile.getName()+ "发生错误!", e);
		}
		
		Element root = document.getRootElement();
		List<Element> elements = root.elements();
		for(Element element : elements){
			String key = element.getName();
			String value = element.getTextTrim();
			setting.put(key, value);
		}
//		String sim_no = root.selectSingleNode("//sim_no").getText().trim();
//		String device_no = root.selectSingleNode("//device_no").getText().trim();
//		String vin_no = root.selectSingleNode("//vin_no").getText().trim();
//		String gateway_ip = root.selectSingleNode("//gateway_ip").getText().trim();
//		String gateway_port = root.selectSingleNode("//gateway_port").getText().trim();
//		setting.put("sim_no", sim_no);
//		setting.put("device_no", device_no);
//		setting.put("vin_no", vin_no);
//		setting.put("gateway_ip", gateway_ip);
//		setting.put("gateway_port", gateway_port);
	}
	
	public static Map<String,String> getSetting(){
		if(setting.size()<1){
			try {
				init();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return setting;
	}
	
}
